#include "../process.h"
#include "../util.h"
#include "model\abbr_model.h"
#include "config.h"

map<string, int> query_list;

void export_model(fstream &out_file, map<string, int> &model)
{
	map<string, int>::iterator itr;
	for(itr = model.begin(); itr != model.end(); ++ itr)
		out_file << (*itr).first << "\t" << (*itr).second << " | ";
	out_file << endl;
}

int main(int argc, const char* argv[])
{
	fstream input(argv[1], fstream::in);
	fstream stop_file(argv[2], fstream::in);
	fstream output(argv[3], fstream::out);

	fstream conf_file(argv[4], fstream::in);
	Config::load(&conf_file);
	conf_file.close();

	AbbrModel abbr(&stop_file);
	stop_file.close();

	while(!input.eof()){
		string line;
		getline(input, line);
		if(line[0] == '{'){
			vector<string> c_prop = util::tokenize(line, "{\t");
			string c_id = c_prop[0];
			int c_size = atoi(c_prop[1].c_str());
			while(!input.eof()){
				getline(input, line);
				if(line[0] == '}'){
					map<string, int> abbr_res;
					abbr.build(query_list, abbr_res);
					export_model(output, abbr_res);
					query_list.clear();
					break;
				}
				int del = line.find('\t');
				string query = line.substr(0, del);
				query_list.insert(pair<string, int>(query, 1));
			}
		}
	}
	input.close();
	output.close();
	return 0;
}